package com.ln.asset.core.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ln.asset.common.result.R;
import com.ln.asset.core.pojo.AssetsFixation;
import com.ln.asset.core.pojo.SupplyUser;
import com.ln.asset.core.service.SupplyUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhaojiaqi
 * @since 2022-04-28
 */
@Api(tags = "供应商")
@RestController
@RequestMapping("/admin/core/supplyUser")
@CrossOrigin
@Slf4j
public class SupplyUserController {

    @Resource
    private SupplyUserService supplyUserService;
/*
* 全查
* */
    @ApiOperation("供应商信息展示")
    @GetMapping("/list")
    public R findALL(){
        List<SupplyUser> list = supplyUserService.list();
        return R.ok().data("list", list);
    }

    /*
    * 分页模糊
    * */
    @ApiOperation("供应商信息展示分页 模糊")
    @GetMapping("/list/{page}/{limit}")
    public R listPage(
            @ApiParam(value = "当前页码", required = true)
            @PathVariable Long page,

            @ApiParam(value = "每页记录数", required = true)
            @PathVariable Long limit,

            @ApiParam(value = "查询关键字", required = false)
            @RequestParam String keyword) {
        //这里的@RequestParam其实是可以省略的，但是在目前的swagger版本中（2.9.2）不能省略，
        //否则默认将没有注解的参数解析为body中的传递的数据
        Page<SupplyUser> pageParam = new Page<>(page, limit);
        IPage<SupplyUser> pageModel = supplyUserService.listPage(pageParam, keyword);
        return R.ok().data("pageModel", pageModel);
    }

    /*
    * 新增
    * */
    @ApiOperation("添加供应商信息展示")
    @PostMapping("/save")
    public R save(@RequestBody SupplyUser supplyUser){
        boolean result = supplyUserService.save(supplyUser);
        if(result){
            return R.ok().message("新增成功");
        }else{
            return  R.error().message("新增失败");
        }
    }
/*
* 根据id查询单条数据
* */
    @ApiOperation("根据id获取固定资产")
    @GetMapping("/get/{id}")
    public R getById(
            @ApiParam(value = "数据id", required = true, example = "1")
            @PathVariable Long id
    ){
        SupplyUser byId = supplyUserService.getById(id);
        if(byId != null){
            return R.ok().data("record", byId);
        }else{
            return R.error().message("数据不存在");
        }
    }

/*
* 修改
* */
    @ApiOperation("更新修改供应商信息")
    @PutMapping("/update")
    public R updateById(

            @ApiParam(value = "供应商信息更改", required = true)
            @RequestBody SupplyUser supplyUser){
        boolean result = supplyUserService.updateById(supplyUser);
        if(result){
            return R.ok().message("修改成功");
        }else{
            return R.error().message("修改失败");
        }
    }

/*
* 删除
* */
    @ApiOperation(value = "根据id删除供应商", notes="逻辑删除")
    @DeleteMapping("/remove/{id}")
    public R removeById(
            @ApiParam(value = "供应商id", required = true, example = "1")
            @PathVariable Integer id){
        boolean result = supplyUserService.removeById(id);
        if(result){
            //return R.setResult(ResponseEnum.UPLOAD_ERROR);
            return R.ok().message("删除成功");
        }else{
            return R.error().message("删除失败");
        }
    }
}

